using System.Collections.Generic;

namespace UI.BankingPanel.Data
{
    /// <summary>
    /// 属相映射工具类
    /// 负责将号码映射到对应的属相
    /// </summary>
    public static class ZodiacHelper
    {
        /// <summary>
        /// 属相名称数组（按正确顺序）
        /// </summary>
        private static readonly string[] ZodiacNames = {
            "蛇", "龙", "兔", "虎", "牛", "鼠", 
            "猪", "狗", "鸡", "猴", "羊", "马"
        };

        /// <summary>
        /// 号码到属相的映射字典
        /// 根据正确的属相顺序：1-蛇, 2-龙, 3-兔, 4-虎, 5-牛, 6-鼠, 7-猪, 8-狗, 9-鸡, 10-猴, 11-羊, 12-马
        /// 然后循环：13-蛇, 14-龙, 15-兔...
        /// </summary>
        private static readonly Dictionary<int, string> NumberToZodiac = new Dictionary<int, string>();

        /// <summary>
        /// 静态构造函数，初始化映射关系
        /// </summary>
        static ZodiacHelper()
        {
            InitializeMapping();
        }

        /// <summary>
        /// 初始化号码到属相的映射关系
        /// </summary>
        private static void InitializeMapping()
        {
            // 为1-49号码建立属相映射
            for (int number = 1; number <= 49; number++)
            {
                // 使用模运算确定属相索引 (number - 1) % 12
                int zodiacIndex = (number - 1) % 12;
                NumberToZodiac[number] = ZodiacNames[zodiacIndex];
            }
        }

        /// <summary>
        /// 根据号码获取对应的属相
        /// </summary>
        /// <param name="number">号码（1-49）</param>
        /// <returns>属相名称</returns>
        public static string GetZodiacByNumber(int number)
        {
            if (NumberToZodiac.TryGetValue(number, out string zodiac))
            {
                return zodiac;
            }
            return "未知";
        }

        /// <summary>
        /// 获取指定属相的所有号码
        /// </summary>
        /// <param name="zodiacName">属相名称</param>
        /// <returns>号码列表</returns>
        public static List<int> GetNumbersByZodiac(string zodiacName)
        {
            List<int> numbers = new List<int>();
            
            foreach (var kvp in NumberToZodiac)
            {
                if (kvp.Value == zodiacName)
                {
                    numbers.Add(kvp.Key);
                }
            }
            
            numbers.Sort();
            return numbers;
        }

        /// <summary>
        /// 获取所有属相名称
        /// </summary>
        /// <returns>属相名称数组</returns>
        public static string[] GetAllZodiacNames()
        {
            return (string[])ZodiacNames.Clone();
        }

        /// <summary>
        /// 检查号码是否有效
        /// </summary>
        /// <param name="number">号码</param>
        /// <returns>是否有效</returns>
        public static bool IsValidNumber(int number)
        {
            return number >= 1 && number <= 49;
        }

        /// <summary>
        /// 获取属相在循环中的索引
        /// </summary>
        /// <param name="zodiacName">属相名称</param>
        /// <returns>索引（0-11），如果不存在返回-1</returns>
        public static int GetZodiacIndex(string zodiacName)
        {
            for (int i = 0; i < ZodiacNames.Length; i++)
            {
                if (ZodiacNames[i] == zodiacName)
                {
                    return i;
                }
            }
            return -1;
        }
    }
} 